<?php

/**
 * This is the long description for the Class
 *
 * @author		Application Support Group
 * @copyright	University of Georgia
 * @package		BAMF
 * @todo		Look into changing the logging default values for easier control of when errors get logged.
 */
class Framework_ErrorHandler extends Framework_Object {


	public function __construct() {
		parent::__construct() ;
	}




	/**
	* Check whether an argument is a PEAR_Error object; optionally log the error.
	*/
	public static function isError( $anObject, $location=null, $doLog=true ) {
		$logger = null ;
		$isError = PEAR::isError( $anObject ) ;


		if( $isError && $doLog ) {

			switch( ERROR_HANDLER_MODE ) {
			case DB_ERROR_MODE:
				$conf = array( 'dsn'=>Framework_DB_Utility::unserializeDsn( FRAMEWORK_DSN ) ) ;
				$conf['identLimit'] = 200  ;
				$logger = &Log::singleton( DB_ERROR_MODE, FRAMEWORK_LOG_TABLE, FRAMEWORK_LOG_IDENT, $conf ) ;
				break ;
			default:
				$logger = &Log::singleton( FILE_ERROR_MODE, FRAMEWORK_LOG_FILE ) ;
				break ;
			}
	
	
			if( !empty( $logger )) {
				$remoteaddr = !empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : "Unknown remote address" ;
				$location = empty( $location ) ? Framework_ErrorHandler::getLocation() : $location ;
				$message = $anObject->getMessage() . $anObject->getDebugInfo() ;
				$messageArray = array( 
					'type'=>'ERROR', 
					'timestamp'=>date( 'c', FRAMEWORK_TIMESTAMP ),
					'location'=>$location,
					'remoteaddr'=>$remoteaddr,
					'sessionid'=> !empty( $_REQUEST[FRAMEWORK_SESSION_NAME] ) ? "{$_REQUEST[FRAMEWORK_SESSION_NAME]}" : "Uninitialized session",
					'message'=>"$message"
				) ;
					#'message'=>htmlentities( $anObject->getDebugInfo(), ENT_QUOTES ), 
			        $xml = new XML_Serializer();
			        $xml->serialize($messageArray) ;
				$theMessage = '<?xml version="1.0" encoding="UTF-8" ?>'."\n" ;
				$theMessage .= $xml->getSerializedData();

				$logger->setIdent( !empty($_REQUEST[FRAMEWORK_SESSION_NAME]) ? $_REQUEST[FRAMEWORK_SESSION_NAME] : "Uninitialized session" ) ;
				$logger->log( $theMessage ) ;
			}
		}

		return $isError ;

	}// end isError





	/**
	* Return a PEAR_Error object; optionally log the error.
	*/
	public static function raiseError( $aMessage, $location=null, $doLog=true ) {
		$logger = null ;
		
		switch( ERROR_HANDLER_MODE ) {
		case DB_ERROR_MODE:
			$conf = array( 'dsn'=>Framework_DB_Utility::unserializeDsn( FRAMEWORK_DSN ) ) ;
			$conf['identLimit'] = 200  ;
			$logger = &Log::singleton( DB_ERROR_MODE, FRAMEWORK_LOG_TABLE, FRAMEWORK_LOG_IDENT, $conf ) ;
			break ;
		default:
			$logger = &Log::singleton( FILE_ERROR_MODE, FRAMEWORK_LOG_FILE ) ;
			break ;
		}

		if( !empty( $logger ) && $doLog ) {
			$remoteaddr = !empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : "Unknown remote address" ;
			$location = empty( $location ) ? Framework_ErrorHandler::getLocation() : $location ;
			$messageArray = array( 
				'type'=>'ERROR', 
				'timestamp'=>date( 'c', FRAMEWORK_TIMESTAMP ),
				'location'=>$location,
				'remoteaddr'=>$remoteaddr,
				'sessionid'=> !empty( $_REQUEST[FRAMEWORK_SESSION_NAME] ) ? "{$_REQUEST[FRAMEWORK_SESSION_NAME]}" : "Uninitialized session",
				'message'=>$aMessage 
			) ;
		        $xml = new XML_Serializer();
		        $xml->serialize($messageArray) ;
			$theMessage = '<?xml version="1.0" encoding="UTF-8" ?>'."\n" ;
		    $theMessage .= $xml->getSerializedData();
			
			$logger->setIdent( !empty($_REQUEST[FRAMEWORK_SESSION_NAME]) ? $_REQUEST[FRAMEWORK_SESSION_NAME] : "Uninitialized session" ) ;
			$logger->log( $theMessage ) ;
		}

		return PEAR::raiseError( $aMessage ) ;

	}// end raiseError




	public static function getLocation() {
		$_output = "" ;
		$module = !empty( $_GET['module'] ) ? $_GET['module'] : '' ;
		$class = !empty( $_GET['class'] ) ? "_".$_GET['class'] : '' ;
		$action = !empty( $_GET['action'] ) ? "_".$_GET['action'] : '' ;

		if( !empty( $module )) { 
			$_output .= $module ; 
		}
		if( !empty( $class )) { 
			$_output .= $class ; 
		}
		if( !empty( $action )) { 
			$_output .= $action ; 
		}

		if( empty( $_output )) { 
			$_output = "generic" ; 
		}
		return $_output ;
	}




	public function __destruct() {
		parent::__destruct() ;
	}

}

?>
